package com.cv.platform.service.collection;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.QueryWrapperX;
import com.cv.infra.api.file.FileApi;
import com.cv.platform.controller.platform.collection.vo.ref.CvVideoCollectionRefRespVO;
import com.cv.platform.controller.platform.collection.vo.ref.CvVideoCollectionRefSaveReqVO;
import com.cv.platform.dal.dataobject.collection.CvVideoCollectionRefDO;
import com.cv.platform.dal.dataobject.video.CvVideoDO;
import com.cv.platform.dal.mapper.collection.CvVideoCollectionRefMapper;
import com.cv.platform.service.video.CvVideoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 视频合集关联业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
@Validated
public class CvVideoCollectionRefServiceImpl implements CvVideoCollectionRefService {

    @Resource
    private CvVideoCollectionRefMapper cvVideoCollectionRefMapper;

    @Resource
    @Lazy
    private CvVideoService cvVideoService;

    @DubboReference
    private FileApi fileApi;

    @Override
    public void saveCvVideoCollectionRef(CvVideoCollectionRefSaveReqVO reqVO) {
        CvVideoCollectionRefDO recordDO = BeanUtils.toBean(reqVO, CvVideoCollectionRefDO.class);
        cvVideoCollectionRefMapper.insertOrUpdate(recordDO);
    }

    @Override
    public void deleteCvVideoCollectionRef(Long collectionId) {
        if (ObjUtil.isNotEmpty(collectionId)) {
            QueryWrapperX<CvVideoCollectionRefDO> wrapper = new QueryWrapperX<CvVideoCollectionRefDO>()
                    .eq("collection_id", collectionId);

            cvVideoCollectionRefMapper.delete(wrapper);
        }
    }

    @Override
    public void deleteCvVideoCollectionRef(Long collectionId, Long videoId) {
        if (ObjUtil.isNotEmpty(collectionId) && ObjUtil.isNotEmpty(videoId)) {
            QueryWrapperX<CvVideoCollectionRefDO> wrapper = new QueryWrapperX<CvVideoCollectionRefDO>()
                    .eq("collection_id", collectionId)
                    .eq("video_id", videoId);

            cvVideoCollectionRefMapper.delete(wrapper);
        }
    }

    @Override
    public List<Long> getVideoIdListByCollectionId(Long collectionId) {
        QueryWrapperX<CvVideoCollectionRefDO> wrapper = new QueryWrapperX<CvVideoCollectionRefDO>()
                .eq("collection_id", collectionId);
        return cvVideoCollectionRefMapper.selectList(wrapper).stream().map(CvVideoCollectionRefDO::getVideoId).toList();
    }

    @Override
    public List<CvVideoCollectionRefRespVO> getCvVideoCollectionRefByVideoId(Long videoId) {
        if (ObjUtil.isEmpty(videoId)) {
            return Collections.emptyList();
        }

        QueryWrapperX<CvVideoCollectionRefDO> wrapper = new QueryWrapperX<CvVideoCollectionRefDO>()
                .eq("video_id", videoId)
                .limitN(1);

        CvVideoCollectionRefDO refDO = cvVideoCollectionRefMapper.selectOne(wrapper);
        if (ObjUtil.isEmpty(refDO)) {
            return Collections.emptyList();
        }

        QueryWrapperX<CvVideoCollectionRefDO> liseWrapper = new QueryWrapperX<CvVideoCollectionRefDO>()
                .eq("collection_id", refDO.getCollectionId());
        
        List<CvVideoCollectionRefDO> cvVideoCollectionRefDOList = cvVideoCollectionRefMapper.selectList(liseWrapper);
        if (CollUtil.isEmpty(cvVideoCollectionRefDOList)) {
            return Collections.emptyList();
        }

        return BeanUtils.toBean(cvVideoCollectionRefDOList, CvVideoCollectionRefRespVO.class);
    }

    @Override
    public String getFirstVideoThumbnailUrlByCollectionId(Long collectionId) {
        if (ObjUtil.isNotEmpty(collectionId)) {
            QueryWrapperX<CvVideoCollectionRefDO> wrapper = new QueryWrapperX<CvVideoCollectionRefDO>()
                    .eq("collection_id", collectionId)
                    .orderByDesc("create_time")
                    .limitN(1);

            CvVideoCollectionRefDO cvVideoCollectionRefDO = cvVideoCollectionRefMapper.selectOne(wrapper);
            if (ObjUtil.isNotEmpty(cvVideoCollectionRefDO) && ObjUtil.isNotEmpty(cvVideoCollectionRefDO.getVideoId())) {
                CvVideoDO video = cvVideoService.getVideoById(cvVideoCollectionRefDO.getVideoId());
                if (ObjUtil.isNotEmpty(video)) {
                    return fileApi.getUrl(video.getThumbnail());
                }
            }
        }
        return StrUtil.EMPTY;
    }

}
